package hotel.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import hotel.action.ForwardActionTo;
/**
 * This class delete session.
 * 
 * @author Alex
 */
public class LogoutAction extends Action 
{
	private static final Logger logger = Logger.getLogger(LogoutAction.class);
	private static final String LOGOUT_ERROR = "Error in LogoutAction: "; 
	
	@Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception 
    {
        String target = ForwardActionTo.ERROR;
        try
        {
			HttpSession session = request.getSession(true);
	        try
	        {
	            session.removeAttribute("user");
	            session.invalidate();
	            target = ForwardActionTo.SUCCESS;
	        }
	        catch(Exception ex)
	        {
	        	target = ForwardActionTo.ERROR;
	        	logger.error(LOGOUT_ERROR + ex.getMessage());
	        }
        }
		 catch(NullPointerException e)
		 {
	 		 logger.error(LOGOUT_ERROR + e);
	 	 }
        
        return mapping.findForward(target);
    }
}
